Skip to content

Conversation

@paulbalandan
Copy link
Member

Description
I remember I have read somewhere in the codebase or in the documentation that model() can accept any object other than those extending CodeIgniter\Model as long as that class follow the semantics of the Model class. So, this relaxes the requirement for model() to accept only Model classes.

Supersedes and closes #9391 .

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@neznaika0
Copy link
Contributor

I looked at Factories - the model object is expected. Are you saying that you can return any object?

@paulbalandan
Copy link
Member Author

Yes, look at the PHPDoc of Fabricator::$model:

/**
* Model instance (can be non-framework if it follows framework design)
*
* @var Model|object
*/
protected $model;

Looking at its constructor, it doesn't check for Model explicitly. Just plain object is fine.

if (is_string($model)) {
// Create a new model instance
$model = model($model, false);
}
if (! is_object($model)) {
throw new InvalidArgumentException(lang('Fabricator.invalidModel'));
}
$this->model = $model;

Lastly, look at the return type of model():

* @phpstan-return ($name is class-string<ModelTemplate> ? ModelTemplate : object|null)

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStan errors are unrelated?

@paulbalandan paulbalandan merged commit 5d1d53a into codeigniter4:develop Mar 18, 2025
6 checks passed
@paulbalandan paulbalandan deleted the model-argument-type branch March 18, 2025 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants